/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bakenote.components;

import org.apache.pivot.wtk.*;
import org.apache.pivot.wtk.Button;
import org.apache.pivot.wtk.Insets;
import org.apache.pivot.wtk.Label;
import org.apache.pivot.wtk.content.ButtonData;

import java.awt.*;

/**
 *
 * @author lello
 */
public class TabButtonDataRenderer extends BoxPane implements Button.DataRenderer {

    protected Label label = new Label();
	private int width = 0;

    public TabButtonDataRenderer(int width) {
        getStyles().put("horizontalAlignment", HorizontalAlignment.LEFT);
        getStyles().put("verticalAlignment", VerticalAlignment.CENTER);

		this.width = width;
		this.setPreferredWidth(width);
        add(label);
		//this.getStyles().put("padding", new Insets(0,2,0,0));
    }

    @Override
    public void setSize(int width, int height) {
        super.setSize(width, height);

        // Since this component doesn't have a parent, it won't be validated
        // via layout; ensure that it is valid here
        validate();
    }

    @Override
    public void render(Object data, Button button, boolean highlighted) {
        String text = null;
		Font font = new Font("Verdana",Font.PLAIN,14);

        if (data instanceof ButtonData) {
            ButtonData buttonData = (ButtonData)data;
            text = buttonData.getText();
        } else {
            if (data != null) {
                text = data.toString();
            }
        }

        label.setText(text);

        if (text == null) {
            label.setVisible(false);
        } else {
            label.setVisible(true);

			if (text.equals("Recipes")) {
				font = new Font("Verdana",Font.ITALIC,14);
			}
			label.getStyles().put("font", font);

            Color color;
            if (button.isEnabled()) {
                color = (Color)button.getStyles().get("color");
            } else {
                color = (Color)button.getStyles().get("disabledColor");
            }

            label.getStyles().put("color", color);
            label.getStyles().put("padding", new Insets(0,3,0,0));
        }

		// Before setting the label text we check if the length of the displayed text
		// is compatible with the length of the Label. If not we remove part of the string
		// and replace it with three dots "..."
		if (label.getPreferredWidth() > width) {
            if (text != null) {
			    int i = text.length()-1;
			while (label.getPreferredWidth() > width) {
				i--;
				String s = text.substring(0, i);
				label.setText(s);
			}
			if (i > 2) i = i-2; // just a padding
			String dottedText = text.substring(0, i)+"...";
			label.setText(dottedText);
            }
		}

    }

    @Override
    public String toString(Object data) {
        String string = null;

        if (data instanceof ButtonData) {
            ButtonData buttonData = (ButtonData)data;
            string = buttonData.getText();
        } else {
            if (data != null) {
                string = data.toString();
            }
        }

        return string;
    }

}
